//
//  HMVisitorView.swift
//  HM22
//
//  Created by Apple on 16/11/29.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

//  定义代理协议 
protocol HMVisitorViewDelegate: NSObjectProtocol {
    func loginOperation()
}

//  自定义访客视图控制器
class HMVisitorView: UIView {

    //  点击登录或者注册执行的回调
    var callBack: (()->())?
    //  代理对象
    weak var delegate: HMVisitorViewDelegate?
    
    //  圆形图片
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //  罩层图片
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //  主页
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //  信息
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 13)
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGray
        label.numberOfLines = 0
        label.textAlignment = .center
        return label
    }()
    
    //  注册
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        button.setTitle("注册", for: .normal)
        button.addTarget(self, action: #selector(btnRegisterAction), for: .touchUpInside)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        return button
    }()
    
    //  登录
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(btnLoginAction), for: .touchUpInside)
        button.setTitle("登录", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //  添加视图设置约束
    private func setupUI() {
        //  设置背景色
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //  使用手写layout方式布局
//        cycleImageView.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
        
        cycleImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(cycleImageView)
            make.top.equalTo(cycleImageView.snp_bottom)
            make.width.equalTo(224)
        }
        
        registerButton.snp_makeConstraints { (make) in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        
        loginButton.snp_makeConstraints { (make) in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
        
        
        
    }
    
    //  开启动画
    private func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //  目的地
        animation.toValue = 2 * M_PI
        //  时长
        animation.duration = 20
        //  次数
        animation.repeatCount = MAXFLOAT
        //  失去焦点的时候不让其动画释放
        animation.isRemovedOnCompletion = false
        //  执行动画
        cycleImageView.layer.add(animation, forKey: nil)
        
    }
    
    //  修改访客视图信息
    func updateVisitorInfo(imageName: String?, message: String?) {
        if imageName != nil && message != nil {
            iconImageView.image = UIImage(named: imageName!)
            messageLabel.text = message!
            //  隐藏圆形图片视图
            cycleImageView.isHidden = true
            
        }else {
            startAnimation()
        }
        
    }
    
    // MARK:    --点击按钮处理逻辑
    @objc private func btnRegisterAction() {
        callBack?()
        delegate?.loginOperation()
    }
    
    @objc private func btnLoginAction() {
        callBack?()
        delegate?.loginOperation()
    }
    
    
    
    
    
    
}
